<template>
  <div class="ranking-container">
    <!-- 顶部标题区 -->
    <div class="header">
      <h2>答题排行榜</h2>
      <p class="subtitle">展示用户答题数量排名</p>
      <router-link to="/training/mock-exam" class="practice-btn">
        <el-icon><EditPen /></el-icon>
        <span>去刷题</span>
      </router-link>
    </div>

    <!-- 当前用户状态 -->
    <div v-if="currentUserRank > 0" class="user-status-card">
      <div class="status-item">
        <span class="label">您的排名：</span>
        <span class="value">{{ currentUserRank }}</span>
      </div>
      <div class="status-item">
        <span class="label">答题总数：</span>
        <span class="value">{{ currentUserCount }}</span>
      </div>
    </div>

    <!-- 排行榜主体 -->
    <div class="ranking-content">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loader"></div>
        <p>加载排行榜数据中...</p>
      </div>

      <!-- 错误提示 -->
      <div v-else-if="error" class="error-container">
        <div class="error-icon">⚠️</div>
        <h3>加载失败</h3>
        <p>{{ error }}</p>
        <button @click="fetchRankings" class="retry-btn">重新加载</button>
      </div>

      <!-- 排行榜表格 -->
      <div v-else class="ranking-table">
        <div class="table-header">
          <div class="rank-col">排名</div>
          <div class="user-col">用户</div>
          <div class="count-col">答题数量</div>
        </div>
        
        <div 
          v-for="(item, index) in rankList" 
          :key="item.userId"
          :class="['table-row', { 
            'current-user': item.userId === currentUserId,
            'top-three': index < 3 
          }]"
        >
          <div class="rank-col">
            <span v-if="index > 2">{{ index + 1 }}</span>
            <span v-else class="medal-icon">
              {{ index === 0 ? '🥇' : index === 1 ? '🥈' : '🥉' }}
            </span>
          </div>
          <div class="user-col">
            <span>{{ item.username }}</span>
            <span v-if="item.userId === currentUserId" class="you-tag">(我)</span>
          </div>
          <div class="count-col">{{ item.practiceCount }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useUserStore } from '@/store/user';
import axios from 'axios';
import { EditPen } from '@element-plus/icons-vue';

const userStore = useUserStore();
const rankList = ref([]);
const loading = ref(true);
const error = ref(null);
const currentUserId = computed(() => userStore.userId);

// 获取当前用户排名
const currentUserRank = computed(() => {
  if (!currentUserId.value || rankList.value.length === 0) return 0;
  
  const userIndex = rankList.value.findIndex(
    item => item.userId === currentUserId.value
  );
  
  return userIndex !== -1 ? userIndex + 1 : 0;
});

// 获取当前用户答题数
const currentUserCount = computed(() => {
  if (!currentUserId.value || rankList.value.length === 0) return 0;
  
  const user = rankList.value.find(
    item => item.userId === currentUserId.value
  );
  
  return user ? user.practiceCount : 0;
});

// 获取排行榜数据
const fetchRankings = async () => {
  try {
    loading.value = true;
    error.value = null;
    
    const response = await axios.get('http://localhost:8080/api/rank/practice');
    
    if (response.data.code === 200 && response.data.data) {
      rankList.value = response.data.data;
    } else {
      throw new Error(response.data.message || '获取排行榜数据失败');
    }
  } catch (err) {
    console.error('获取排行榜失败:', err);
    error.value = err.message || '无法连接到服务器，请稍后再试';
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchRankings();
});
</script>

<style scoped>
.ranking-container {
  max-width: 900px;
  margin: 20px auto;
  padding: 30px;
  font-family: 'Helvetica Neue', Arial, sans-serif;
  color: #333;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.header {
  text-align: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eaeaea;
  position: relative;
}

.header h2 {
  font-size: 28px;
  font-weight: 600;
  color: #1a365d;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 16px;
  color: #718096;
  margin-bottom: 20px;
}

.practice-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background-color: #409EFF;
  color: white;
  border-radius: 6px;
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s;
  margin-top: 10px;
}

.practice-btn:hover {
  background-color: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.practice-btn .el-icon {
  margin-right: 8px;
  font-size: 18px;
}

.user-status-card {
  background-color: #f8f9fa;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 30px;
  display: flex;
  justify-content: center;
  gap: 40px;
}

.status-item {
  display: flex;
  align-items: center;
}

.label {
  font-size: 16px;
  color: #4a5568;
}

.value {
  font-size: 20px;
  font-weight: 600;
  color: #2b6cb0;
  margin-left: 8px;
}

.loading-container, .error-container {
  padding: 50px 20px;
  text-align: center;
}

.loader {
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-container {
  color: #e53e3e;
}

.error-icon {
  font-size: 3rem;
  margin-bottom: 15px;
}

.retry-btn {
  background-color: #3182ce;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  margin-top: 15px;
  transition: all 0.2s;
}

.retry-btn:hover {
  background-color: #2b6cb0;
  transform: translateY(-1px);
}

.ranking-table {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.table-header {
  display: flex;
  background-color: #edf2f7;
  font-weight: 600;
  padding: 16px 20px;
  border-bottom: 1px solid #e2e8f0;
  font-size: 16px;
}

.table-row {
  display: flex;
  padding: 16px 20px;
  border-bottom: 1px solid #e2e8f0;
  transition: all 0.2s;
  font-size: 15px;
}

.table-row:last-child {
  border-bottom: none;
}

.table-row:hover {
  background-color: #f7fafc;
  transform: translateX(3px);
}

.table-row.current-user {
  background-color: #ebf8ff;
  border-left: 4px solid #3182ce;
}

.rank-col {
  width: 100px;
  font-weight: 500;
}

.user-col {
  flex: 1;
  display: flex;
  align-items: center;
}

.count-col {
  width: 120px;
  text-align: right;
  font-weight: 500;
}

.medal-icon {
  font-size: 20px;
}

.you-tag {
  background-color: #3182ce;
  color: white;
  font-size: 14px;
  padding: 3px 10px;
  border-radius: 12px;
  margin-left: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .ranking-container {
    padding: 20px;
    margin: 10px;
  }
  
  .header h2 {
    font-size: 24px;
  }
  
  .user-status-card {
    flex-direction: column;
    gap: 15px;
    align-items: center;
    padding: 15px;
  }
  
  .table-header, .table-row {
    padding: 12px 15px;
    font-size: 14px;
  }
  
  .rank-col {
    width: 80px;
  }
  
  .count-col {
    width: 100px;
  }
  
  .practice-btn {
    padding: 8px 16px;
    font-size: 14px;
  }
}
</style>